﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;
using Entities;
using Common.Constants;
using System.Globalization;
namespace ServicesDirectory.UserControl
{
    public partial class ManagerContact : System.Web.UI.UserControl
    {
        /// <summary>
        /// Occurs when [close control].
        /// </summary>
        public event EventHandler CloseControl;
        public int ActionForm { get; set; }
        public string ContactId { get; set; }

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            txtFirstName.TextChanged += TxtFirstName_TextChanged;
            txtSurname.TextChanged += TxtSurname_TextChanged;

            cboContactType.DataSource = ContactBusiness.Instance.GetContactType();
            cboContactType.DataTextField = "DataRef";
            cboContactType.DataValueField = "RefValue";
            cboContactType.DataBind();
            cboBestContact.DataSource = ContactBusiness.Instance.BestContactMethod();
            cboBestContact.DataTextField = "DataRef";
            cboBestContact.DataValueField = "RefValue";
            cboBestContact.DataBind();
            const string draggable = "$( '#draggableLookUpManager' ).draggable();";
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), "OrganisationDetailKeyContactSearch", draggable, true);
           

            LookupContactManager1.GetManager += LookupContactManager1_GetManager;
            LookupContactManager1.CloseControl += LookupContactManager1_CloseControl;
            if (ViewState["actionForm"] != null && int.Parse(ViewState["actionForm"].ToString(), CultureInfo.InvariantCulture) == (int)ModeForm.Update)
                lblTitleContact.Text = "Update contact";
            else
            {
                lblTitleContact.Text = "Add new contact";

            }
            if(lookupc.Visible)
            {
                const string draggable1 = "$( '#overlay1' ).show();";
                ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), "ManagerContactKeyContactEnable", draggable1, true);
            }
            if (!IsPostBack)
                txtFirstName.Focus();
        }

        /// <summary>
        /// Handles the TextChanged event of the TxtSurname control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void TxtSurname_TextChanged(object sender, EventArgs e)
        {
            lblannounce.Visible = false;
        }

        /// <summary>
        /// Handles the TextChanged event of the TxtFirstName control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void TxtFirstName_TextChanged(object sender, EventArgs e)
        {
            lblannounce.Visible = false;

        }

        /// <summary>
        /// Handles the CloseControl event of the LookupContactManager1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        private void LookupContactManager1_CloseControl(object sender, EventArgs e)
        {
            const string draggable = "$( '#overlay1' ).hide();";
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), "ManagerContactKeyContactvisible1", draggable, true);
            lookupc.Visible = false;

        }

        /// <summary>
        /// Handles the GetManager event of the LookupContactManager1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        private void LookupContactManager1_GetManager(object sender, EventArgs e)
        {
            lblannounce.Visible = false;
            string message;
            var contactManager = ContactBusiness.Instance.GetContact(sender.ToString(),
                                                                     out message);
            if (contactManager != null)
            {
                lblManagerName.Text = contactManager.FirstName + " " + contactManager.SurName;
                lblManagerName.ToolTip = lblManagerName.Text;
                lblManagerName.Text = lblManagerName.Text.Length > 15
                                          ? lblManagerName.Text.Substring(0, 12) + "..."
                                          : lblManagerName.Text;
                lookup.CommandArgument = contactManager.ContactId.ToString();

            }
            else
            {
                lblManagerName.Text = "";
                lookup.CommandArgument = Guid.Empty.ToString();
            }

        }

        /// <summary>
        /// Handles the Click event of the lookup control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void Lookup_Click(object sender, EventArgs e)
        {
            lookupc.Visible = true;
            const string draggable = "$( '#overlay1' ).show();";
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), "ManagerContactKeyContactEnable1", draggable, true); 

        }

        /// <summary>
        /// Handles the Click event of the cmdBack control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void CmdBack_Click(object sender, EventArgs e)
        {
            if (ViewState["contactId"] != null)
                ContactId = ViewState["contactId"].ToString();
            string action = "";
            if (ViewState["actionForm"] != null)
                action = ViewState["actionForm"].ToString();
            if (CloseControl != null)
            {

                txtFirstName.Text = "";
                txtSurname.Text = "";
                lblannounce.Visible = false;
                txtJobRole.Text = "";
                txtJobTitle.Text = "";
                txtKnownAs.Text = "";
                txtMobilePhone.Text = "";
                txtOfficePhone.Text = "";
                txtStHomePhone.Text = "";
                txtWorkbase.Text = "";
                txtEmail.Text = "";
                lblManagerName.Text = string.Empty;
                lookup.CommandArgument = string.Empty;
                CloseControl("100", e);
            }

        }


        /// <summary>
        /// Binds the contact.
        /// </summary>
        public void BindContact()
        {
            lblTitleContact.Text = "Update contact";
            string message;
            var contact = ContactBusiness.Instance.GetContact(ContactId, out message);
            if (contact != null)
            {

                txtEmail.Text = HttpUtility.HtmlDecode(contact.Email);
                txtFirstName.Text = HttpUtility.HtmlDecode(contact.FirstName);
                txtJobRole.Text = HttpUtility.HtmlDecode(contact.JobRole);
                txtJobTitle.Text = HttpUtility.HtmlDecode(contact.JobTitle);
                txtKnownAs.Text = HttpUtility.HtmlDecode(contact.KnownAs);
                txtMobilePhone.Text = HttpUtility.HtmlDecode(contact.MobilePhone);
                txtOfficePhone.Text = HttpUtility.HtmlDecode(contact.OfficePhone);
                txtStHomePhone.Text = HttpUtility.HtmlDecode(contact.StHomePhone);
                txtSurname.Text = HttpUtility.HtmlDecode(contact.SurName);
                txtWorkbase.Text = HttpUtility.HtmlDecode(contact.Workbase);
                if (contact.BestContactType!=0)
                cboBestContact.SelectedValue = contact.BestContactType.ToString(CultureInfo.InvariantCulture);
                if (contact.ContactType!= 0)
                cboContactType.SelectedValue = contact.ContactType.ToString(CultureInfo.InvariantCulture);
                chkActive.Checked = contact.IsActive;
                if (contact.ManagerId.ToString() != string.Empty && contact.ManagerId != Guid.Empty)
                {
                    var contactManager = ContactBusiness.Instance.GetContact(contact.ManagerId.ToString(),
                                                                             out message);

                    if (contactManager != null)
                    {
                        lblManagerName.Text = contactManager.FirstName + " " + contactManager.SurName;
                    }
                }

                ViewState["contactId"] = ContactId;
            }

        }
        /// <summary>
        /// Sets the mode form.
        /// </summary>
        /// <param name="modeForm">The mode form.</param>
        public void SetModeForm(string modeForm)
        {
            ViewState["actionForm"] = modeForm;
        }

        /// <summary>
        /// Handles the Click event of the cmdSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void CmdSave_Click(object sender, EventArgs e)
        {
            int actionForm;

            if (ViewState["actionForm"] != null && int.TryParse(ViewState["actionForm"].ToString(), out actionForm))
            {

                if (actionForm == (int)ModeForm.Update)
                {

                    var contact = new Contact();
                    contact.ContactId = new Guid(ViewState["contactId"].ToString());
                    int bestContactMethod;
                    if (int.TryParse(cboBestContact.SelectedValue, out bestContactMethod))
                        contact.BestContactType = bestContactMethod;
                    int typeContact;
                    if (int.TryParse(cboContactType.SelectedValue, out typeContact))
                        contact.ContactType = typeContact;
                    contact.Email = HttpUtility.HtmlEncode(txtEmail.Text);
                    contact.FirstName = HttpUtility.HtmlEncode(txtFirstName.Text);
                    contact.IsActive = chkActive.Checked;
                    contact.JobRole = HttpUtility.HtmlEncode(txtJobRole.Text);
                    contact.JobTitle = HttpUtility.HtmlEncode(txtJobTitle.Text);
                    contact.KnownAs = HttpUtility.HtmlEncode(txtKnownAs.Text);
                    if (!string.IsNullOrEmpty(lookup.CommandName))
                        contact.ManagerId = new Guid(lookup.CommandArgument);
                    contact.MobilePhone = HttpUtility.HtmlEncode(txtMobilePhone.Text);
                    contact.OfficePhone = HttpUtility.HtmlEncode(txtOfficePhone.Text);
                    contact.StHomePhone = HttpUtility.HtmlEncode(txtStHomePhone.Text);
                    contact.SurName = HttpUtility.HtmlEncode(txtSurname.Text);
                    contact.Workbase = HttpUtility.HtmlEncode(txtWorkbase.Text);
                    if (!ContactBusiness.Instance.CheckContactName(contact.FirstName, contact.SurName, contact.ContactId))
                    {
                        var message = ContactBusiness.Instance.Update(contact);

                        if (message == UIMessage.UpdateContactSuccess)
                        {
                            ContactId = contact.ContactId.ToString();
                            if (CloseControl != null)
                            {
                                txtFirstName.Text = "";
                                txtSurname.Text = "";
                                lblannounce.Visible = false;
                                txtJobRole.Text = "";
                                txtJobTitle.Text = "";
                                txtKnownAs.Text = "";
                                txtMobilePhone.Text = "";
                                txtOfficePhone.Text = "";
                                txtStHomePhone.Text = "";
                                txtWorkbase.Text = "";
                                txtEmail.Text = "";
                                lblManagerName.Text = string.Empty;
                                lookup.CommandArgument = string.Empty;
                                CloseControl(((int)ModeForm.Update).ToString(CultureInfo.InvariantCulture), e);
                            }

                        }
                    }
                    else
                    {
                        var message = "Contact Name can not dulicate!";
                        txtFirstName.Focus();
                        lblannounce.Text = message;
                        lblannounce.Visible = true;
                    }



                }
                if (actionForm == (int)ModeForm.Add)
                {


                    var contact = new Contact();
                    contact.ContactId = Guid.NewGuid();
                    int bestContactMethod;
                    if (int.TryParse(cboBestContact.SelectedValue, out bestContactMethod))
                        contact.BestContactType = bestContactMethod;
                    int typeContact;
                    if (int.TryParse(cboContactType.SelectedValue, out typeContact))
                        contact.ContactType = typeContact;
                    contact.Email = HttpUtility.HtmlEncode(txtEmail.Text);
                    contact.FirstName = HttpUtility.HtmlEncode(txtFirstName.Text);
                    contact.IsActive = chkActive.Checked;
                    contact.JobRole = HttpUtility.HtmlEncode(txtJobRole.Text);
                    contact.JobTitle = HttpUtility.HtmlEncode(txtJobTitle.Text);
                    contact.KnownAs = HttpUtility.HtmlEncode(txtKnownAs.Text);
                    if (!string.IsNullOrEmpty(lookup.CommandName))
                        contact.ManagerId = new Guid(lookup.CommandArgument);
                    contact.MobilePhone = HttpUtility.HtmlEncode(txtMobilePhone.Text);
                    contact.OfficePhone = HttpUtility.HtmlEncode(txtOfficePhone.Text);
                    contact.StHomePhone = HttpUtility.HtmlEncode(txtStHomePhone.Text);
                    contact.SurName = HttpUtility.HtmlEncode(txtSurname.Text);
                    contact.Workbase = HttpUtility.HtmlEncode(txtWorkbase.Text);
                    if (!ContactBusiness.Instance.CheckContactName(contact.FirstName, contact.SurName, contact.ContactId))
                    {
                        var message = ContactBusiness.Instance.Insert(contact);
                        ViewState["contactId"] = contact.ContactId;

                        if (message == UIMessage.InsertContactSuccess)
                        {
                            ContactId = contact.ContactId.ToString();
                            if (CloseControl != null)
                            {
                                txtFirstName.Text = "";
                                txtSurname.Text = "";
                                lblannounce.Visible = false;
                                txtJobRole.Text = "";
                                txtJobTitle.Text = "";
                                txtKnownAs.Text = "";
                                txtMobilePhone.Text = "";
                                txtOfficePhone.Text = "";
                                txtStHomePhone.Text = "";
                                txtWorkbase.Text = "";
                                txtEmail.Text = "";
                                lblManagerName.Text = string.Empty;
                                lookup.CommandArgument = string.Empty;
                                CloseControl(((int)ModeForm.Add).ToString(CultureInfo.InvariantCulture), e);
                            }
                        }

                    }
                    else
                    {
                        var message = "Contact Name can not dulicate!";
                        txtFirstName.Focus();
                        lblannounce.Text = message;
                        lblannounce.Visible = true;
                    }

                }
            }
            else
            {



                var contact = new Contact();
                contact.ContactId = Guid.NewGuid();
                int bestContactMethod;
                if (int.TryParse(cboBestContact.SelectedValue, out bestContactMethod))
                    contact.BestContactType = bestContactMethod;
                int typeContact;
                if (int.TryParse(cboContactType.SelectedValue, out typeContact))
                    contact.ContactType = typeContact;
                contact.Email = HttpUtility.HtmlEncode(txtEmail.Text);
                contact.FirstName = HttpUtility.HtmlEncode(txtFirstName.Text);
                contact.IsActive = chkActive.Checked;
                contact.JobRole = HttpUtility.HtmlEncode(txtJobRole.Text);
                contact.JobTitle = HttpUtility.HtmlEncode(txtJobTitle.Text);
                contact.KnownAs = HttpUtility.HtmlEncode(txtKnownAs.Text);
                if (!string.IsNullOrEmpty(lookup.CommandName))
                    contact.ManagerId = new Guid(lookup.CommandArgument);
                contact.MobilePhone = HttpUtility.HtmlEncode(txtMobilePhone.Text);
                contact.OfficePhone = HttpUtility.HtmlEncode(txtOfficePhone.Text);
                contact.StHomePhone = HttpUtility.HtmlEncode(txtStHomePhone.Text);
                contact.SurName = HttpUtility.HtmlEncode(txtSurname.Text);
                contact.Workbase = HttpUtility.HtmlEncode(txtWorkbase.Text);
                if (!ContactBusiness.Instance.CheckContactName(contact.FirstName, contact.SurName, contact.ContactId))
                {
                    var message = ContactBusiness.Instance.Insert(contact);
                    ViewState["contactId"] = contact.ContactId;

                    if (message == UIMessage.InsertContactSuccess)
                        if (CloseControl != null)
                        {
                            txtFirstName.Text = "";
                            txtSurname.Text = "";
                            lblannounce.Visible = false;
                            txtJobRole.Text = "";
                            txtJobTitle.Text = "";
                            txtKnownAs.Text = "";
                            txtMobilePhone.Text = "";
                            txtOfficePhone.Text = "";
                            txtStHomePhone.Text = "";
                            txtWorkbase.Text = "";
                            txtEmail.Text = "";
                            lblManagerName.Text = string.Empty;
                            lookup.CommandArgument = string.Empty;
                            CloseControl(((int)ModeForm.Add).ToString(CultureInfo.InvariantCulture), e);
                        }
                }
                else
                {
                    var message = "Contact Name can not dulicate!";
                    txtFirstName.Focus();
                    lblannounce.Text = message;
                    lblannounce.Visible = true;
                }
            }

        }
    }
}